<span class="absolute flex items-center justify-center w-8 h-8 {% if current_guide %}bg-green-200{% else %}bg-slate-200{% endif %} rounded-full -start-4 ring-4 ring-white dark:ring-slate-900 dark:bg-slate-700">
    <form>
        <input type="hidden" name="chat_id" value="{{ current_chat.chat_id }}">
        <svg hx-post="{% url 'generate_onboarding_guide' %}"
             hx-trigger="generate-guide from:body"
             hx-indicator="closest li"
             hx-target="closest li"
             xmlns="http://www.w3.org/2000/svg"
             fill="none"
             viewBox="0 0 24 24"
             stroke-width="1.5"
             stroke="currentColor"
             class="w-3.5 h-3.5 htmx-request-hidden {% if current_guide %}text-green-900{% endif %}">
            <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
        </svg>
    </form>
    <span class="hidden htmx-request-block loading loading-infinity loading-sm"></span>
</span>
<h3 class="font-medium leading-tight {% if current_guide %}text-slate-800{% endif %}">Onboarding guide</h3>
<p class="text-sm">
    <span class="htmx-request-hidden">Review the onboarding guide</span>
    <span class="hidden ellipsis htmx-request-inline">Generating onboarding guide</span>
</p>
{% if final_template %}
    <div class="mt-3">
        <div class="flex flex-col items-end">
            <input type="hidden" name="chat_id" value="{{ current_chat.chat_id }}">
            <textarea name="template" id="draft-template"></textarea>
            <script>
        if (typeof guideEditor !== 'undefined') {
            isGuideEditorFullScreen = guideEditor.codemirror.getOption('fullScreen')
            if (isGuideEditorFullScreen) {
                guideEditor.toggleFullScreen()
            }
        }
        guideEditor = new EasyMDE({
            previewRender: function(plainText) {
                let html = this.parent.markdown(plainText);

                // Add "hljs" class to code blocks
                html = html.replace(/<pre><code class="([^"]+)">/g, function(match, p1) {
                    return '<pre class="not-prose"><code class="' + p1 + ' hljs not-prose">';
                });

                html = '<div class="prose prose-sm">' + html + '</div>';

                return html
            },
            status: false,
            renderingConfig: {
                codeSyntaxHighlighting: true
            },
            spellChecker: false,
            // autosave: {
            //     enabled: true,
            //     uniqueId: "{{ current_chat.chat_id }}_guide",
            // },
            forceSync: true,
            maxHeight: "500px",
            initialValue: `{{ current_guide|default:"" }}`,
            element: document.currentScript.closest('div').querySelector('textarea'),
        });
        guideEditor.togglePreview()

        if (typeof guideWebSocket === 'undefined') {
            guideWebSocket = new WebSocket(
                'ws://'
                + window.location.host
                + '/ws/guide/{{ current_chat.chat_id }}'
                + '/'
            );
            guideWebSocket.onmessage = function(event) {
                let data = JSON.parse(event.data);
                if (data.type === "guide.update") {
                    guideEditor.value(data.guide);
                }
            }
        }

</script>
</div>
</div>
{% endif %}
